package balmysundaycandy.more.low.level.image;

import com.google.appengine.api.images.ImagesServicePb.ImagesCompositeRequest;
import com.google.appengine.api.images.ImagesServicePb.ImagesCompositeResponse;
import com.google.appengine.api.images.ImagesServicePb.ImagesHistogramRequest;
import com.google.appengine.api.images.ImagesServicePb.ImagesHistogramResponse;
import com.google.appengine.api.images.ImagesServicePb.ImagesTransformRequest;
import com.google.appengine.api.images.ImagesServicePb.ImagesTransformResponse;
import com.google.appengine.repackaged.com.google.io.protocol.ProtocolMessage;
import com.google.apphosting.api.ApiProxy;

/**
 * image operations.
 * 
 * @author marblejenka
 * 
 * @param <Request>
 * @param <Response>
 */
abstract class ImageOperation<Request extends ProtocolMessage<?>, Response extends ProtocolMessage<?>> {
	private static final String packageName = "images";

	public abstract Response call(Request request);

	public static final class TransformOperation extends ImageOperation<ImagesTransformRequest, ImagesTransformResponse> {
		@Override
		public ImagesTransformResponse call(ImagesTransformRequest request) {
			ImagesTransformResponse response = new ImagesTransformResponse();
			response.mergeFrom(ApiProxy.makeSyncCall(packageName, "Transform", request.toByteArray()));
			return response;
		}
	}

	public static final class CompositeOperation extends ImageOperation<ImagesCompositeRequest, ImagesCompositeResponse> {
        @Override
		public ImagesCompositeResponse call(ImagesCompositeRequest request) {
        	ImagesCompositeResponse response = new ImagesCompositeResponse();
			response.mergeFrom(ApiProxy.makeSyncCall(packageName, "Composite", request.toByteArray()));
			return response;
		}
	}

	public static final class HistogramOperation extends ImageOperation<ImagesHistogramRequest, ImagesHistogramResponse> {
		@Override
		public ImagesHistogramResponse call(ImagesHistogramRequest request) {
			ImagesHistogramResponse response = new ImagesHistogramResponse();
			response.mergeFrom(ApiProxy.makeSyncCall(packageName, "Histogram", request.toByteArray()));
			return response;
		}
	}
}
